package cn.sccl.dirms.security.service.impl;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.model.QueryData;
import cn.sccl.dirms.security.dao.UserAndRoleDAO;
import cn.sccl.dirms.security.model.Role;
import cn.sccl.dirms.security.model.RoleExtend;
import cn.sccl.dirms.security.service.UserAndRoleManager;
import cn.sccl.common.dao.BaseDAO;
import cn.sccl.common.service.impl.BaseManagerImpl;
import cn.sccl.dirms.security.model.UserAndRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by 杨浩宇 on 2016-02-24.
 */
@Service
public class UserAndRoleManagerImpl extends BaseManagerImpl<UserAndRole> implements UserAndRoleManager {

    @Autowired
    private UserAndRoleDAO userAndRoleDAO;

    public UserAndRoleDAO getUserAndRoleDAO() {
        return userAndRoleDAO;
    }

    @Override
    public BaseDAO<UserAndRole> getBaseDAO() {
        return getUserAndRoleDAO();
    }


    @Override
    public PageResult<Role> getUserAvailRoleInfo(long pageIndex, int pageSize, QueryData queryData) {

        String sql = " SELECT r.id,\n" +
                "               r.name,\n" +
//                "               r.descn,\n" +
                "               r.create_time,\n" +
                "               r.update_time,\n" +
                "               r.delete_flag,\n" +
//                "               r.code,\n" +
//                "               r.apply_province,\n" +
//                "               r.create_date,\n" +
//                "               r.create_user,\n" +
//                "               r.last_update_date,\n" +
//                "               r.last_update_user,\n" +
                "               r.note,\n" +
                "               r.active\n" +
                "          FROM t_sec_role r, t_sec_user u\n" +
                "         WHERE r.delete_flag = 0\n" +
                "           and u.delete_flag = 0\n" +
                "           and r.active = 1\n" +
                "           and u.id = ? \n" +
//                "           and r.name like '%?%'\n" +
                "           and not exists (select 1\n" +
                "                  from t_sec_users_roles ru\n" +
                "                 where r.id = ru.role_id\n" +
                "                   and ru.delete_flag = 0\n" +
                "                   and ru.user_id = u.id)";

        String name = (String) queryData.get("name");

        if (StringUtils.isEmpty(name)) {
            name = "";
        }
        String id = (String) queryData.get("id");
        if (StringUtils.isEmpty(id)) {
            id = "";
        }
        Object[] param = new Object[]{id};

        return getDataBase().query4Model(sql, pageIndex, pageSize, Role.class, param);
    }

    @Override
    public PageResult<RoleExtend> getUserRoleInfo(long pageIndex, int pageSize, QueryData queryData){
        String sql = "SELECT distinct\n" +
                "         r.id,\n" +
                "         ru.id as ruid,\n"+
                "         r.name,\n" +
//                "         r.descn,\n" +
                "         r.create_time,\n" +
                "         r.update_time,\n" +
                "         r.delete_flag,\n" +
//                "         r.code,\n" +
//                "         r.apply_province,\n" +
//                "         r.create_date,\n" +
//                "         r.create_user,\n" +
//                "         r.last_update_date,\n" +
//                "         r.last_update_user,\n" +
                "         r.note,\n" +
                "         r.active\n" +
                "         FROM t_sec_role r,t_sec_user u,t_sec_users_roles ru        \n" +
                "         WHERE r.delete_flag = 0\n" +
                "         and u.delete_flag = 0\n" +
                "         and ru.delete_flag = 0\n" +
                "         and r.id = ru.role_id\n" +
                "         and ru.user_id = u.id\n" +
                "         and u.id=? \n" +
                "         and r.name like '%?%'";

        String name = (String)queryData.get("name");
        String id = (String) queryData.get("id");
        if(StringUtils.isEmpty(name)){
            name="";
        }
        if(StringUtils.isEmpty(id)){
            id="";
        }
        Object[] param = new Object[]{id,name};

        return getDataBase().query4Model(sql ,pageIndex,pageSize,RoleExtend.class,param);

    }
}